package com.budwk.app.sqfwDoc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.exception.Asserts;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfwDoc.models.SqfwTemplateField;
import com.budwk.app.sqfwDoc.models.SqfwTemplateFieldRelation;
import com.budwk.app.sqfwDoc.service.SqfwTemplateFieldRelationService;
import com.budwk.app.sqfwDoc.service.SqfwTemplateFieldService;
import com.budwk.app.sqfwDoc.vo.SqfwTemplateFieldVO;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;
import java.util.List;

/**
 * @author ZY.Wang
 * @since 2023/12/5
 */
@IocBean(args = {"refer:dao"})
public class SqfwTemplateFieldServiceImpl extends BaseServiceImpl<SqfwTemplateField> implements SqfwTemplateFieldService {
    public SqfwTemplateFieldServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private SqfwTemplateFieldRelationService sqfwTemplateFieldRelationService;

    @Override
    public Pagination listConditionPage(SqfwTemplateField field, Integer pageNo, Integer pageSize) {

        Cnd cnd = Cnd.NEW();
        if (StringUtils.isNotBlank(field.getFieldName())) {
            cnd.and(SqfwTemplateField::getFieldName, "like", StrFormatter.format("%{}%", field.getFieldName()));
        }
        if (StringUtils.isNotBlank(field.getFieldCode())) {
            cnd.and(SqfwTemplateField::getFieldCode, "like", StrFormatter.format("%{}%", field.getFieldCode()));
        }

        cnd.desc(SqfwTemplateField::getCreateTime);
        cnd.asc(SqfwTemplateField::getFieldName);

        Pagination pagination = listPage(pageNo, pageSize, cnd);
        List<SqfwTemplateField> list = pagination.getList(SqfwTemplateField.class);

        List<SqfwTemplateFieldVO> resultList = Lists.newArrayList();

        for (SqfwTemplateField itemField : list) {
            Cnd relationDictCnd = Cnd.NEW();
            relationDictCnd.and(SqfwTemplateFieldRelation::getTemplateFieldId, "=", itemField.getId());
            relationDictCnd.andNot(SqfwTemplateFieldRelation::getTemplateDictId, "=", null);
            int dictUseCount = sqfwTemplateFieldRelationService.count(relationDictCnd);

            Cnd relationVisDictCnd = Cnd.NEW();
            relationVisDictCnd.and(SqfwTemplateFieldRelation::getTemplateFieldId, "=", itemField.getId());
            relationVisDictCnd.andNot(SqfwTemplateFieldRelation::getTemplateVisDictId, "=", null);
            int visDictUseCount = sqfwTemplateFieldRelationService.count(relationVisDictCnd);

            SqfwTemplateFieldVO vo = new SqfwTemplateFieldVO();
            BeanUtil.copyProperties(itemField, vo);
            vo.setDictUseCount(dictUseCount);
            vo.setVisDictUseCount(visDictUseCount);
            resultList.add(vo);
        }

        pagination.setList(resultList);
        return pagination;
    }

    @Override
    public void addField(SqfwTemplateField data) {
        String userId = SecurityUtil.getUserId();

        Cnd existCnd = Cnd.NEW();
        existCnd.and(SqfwTemplateField::getFieldName, "=", data.getFieldName());
        int fieldNameCount = count(existCnd);
        if (fieldNameCount > 0) throw new ServiceException("字段名称重复");

        existCnd = Cnd.NEW();
        existCnd.and(SqfwTemplateField::getFieldCode, "=", data.getFieldCode());
        int filedCodeCount = count(existCnd);
        if (filedCodeCount > 0) throw new ServiceException("字段编码重复!");

        data.setCreateBy(userId);
        data.setCreateTime(new Date());
        data.setUpdateBy(userId);
        data.setUpdateTime(new Date());
        insert(data);
    }

    @Override
    public void updateField(SqfwTemplateField data) {
        String userId = SecurityUtil.getUserId();

        Cnd existCnd = Cnd.NEW();
        if (StringUtils.isNotBlank(data.getFieldName())) {
            existCnd.and(SqfwTemplateField::getFieldName, "=", data.getFieldName()).and(SqfwTemplateField::getId, "!=", data.getId());
            int fieldNameCount = count(existCnd);
            if (fieldNameCount > 0) Asserts.fail("名称重复");
        }
        existCnd = Cnd.NEW();
        if (StringUtils.isNotBlank(data.getFieldCode())) {
            existCnd.and(SqfwTemplateField::getFieldCode, "=", data.getFieldCode()).and(SqfwTemplateField::getId, "!=", data.getId());
            int fieldCodeCount = count(existCnd);
            if (fieldCodeCount > 0) Asserts.fail("名称重复");
        }

        data.setUpdateBy(userId);
        data.setUpdateTime(new Date());
        updateIgnoreNull(data);
    }
}
